package cn.lh.spider.job;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import cn.lh.pipeline.MysqlPipeline;
import cn.lh.spider.PolicySpider;
import cn.lh.spider.dao.ITask;
import cn.lh.spider.entity.Task;
import tool.StaticTools;
import us.codecraft.webmagic.Spider;

public class MyJob{
	@Resource
	private ITask taskS;
	public void doWork(){
		
		//查到所有任务为启动的任务id
		//new一个爬虫，将id传入
		int count = taskS.countTask();
		int i = count/10+((count%10==0)?0:1);
		for(int page=0,rows=10;page<i;page++){
			List<Task> taskList = taskS.getData(page*rows, rows);
			Iterator<Task> iter = taskList.iterator();
			while(iter.hasNext()){
				Task task = iter.next();
				if(task.getTaskstate().equals("启动")){
					System.out.println("==================================================");
					System.out.println("=========         一次信息采集开始                      ===========");
					System.out.println("==================================================");
					
					MysqlPipeline.insCount = 0;
					Spider.create(new PolicySpider(task))
					.addUrl(task.getEntryurl())
					//.addUrl("http://www.liangjiang.gov.cn/Content/2012-05/11/content_4158_2.htm")
					//.addUrl("http://www.liangjiang.gov.cn/Content/2015-12/17/content_225523.htm")
			        .addPipeline(new MysqlPipeline())
					//.addPipeline(new FilePipeline("d://a"))
			        .thread(5)
			        .run();
					try {
						Connection conn = StaticTools.getDataSource().getConnection();
						String sql = "UPDATE t_s_sms set es_content='更新"+MysqlPipeline.insCount+"条数据',es_sendtime=NOW() WHERE es_receiver='msg'";
						 PreparedStatement ps = conn.prepareStatement(sql);
						 ps.execute();
						ps.close();
						conn.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					System.out.println("==================================================");
					System.out.println("=========         一次信息采集结束                      ===========");
					System.out.println("==================================================");
				}
			}
		}
		
//		Task task = taskS.findById("59ac07e5c55340f9984b59a60b90dac9");
		
		
		
		
		
	}

	
	

	
}	